﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Water.Architecture2.IDAL;
using System.Reflection;

namespace WaterLive.App_Foundtion
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    public class Autocomplete : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //压缩,
            Water.Web.PageManager.StreamCompress(context);

            //url参数
            string q = context.Request.QueryString["q"];
            string matchField = context.Request.QueryString["MatchField"];
            bool matchCase = Convert.ToBoolean(context.Request.QueryString["MatchCase"]);
            string typeName = context.Request.QueryString["TypeName"];
            string limit = context.Request.QueryString["limit"];
            string strMethod = context.Request.QueryString["MethodName"];

            //反射对象
            Type type = System.Web.Compilation.BuildManager.GetType(typeName, true, true); //Type.GetType(typeName);
            MethodInfo method = null;
            string json = string.Empty;
            //方法参数
            Water.Architecture2.IDAL.WhereFields whereFields = new Water.Architecture2.IDAL.WhereFields();
            if(matchCase)
                whereFields.Add(matchField.ToUpper(), OperatorType.Like, "%" + q + "%");
            else
                whereFields.Add(matchField.ToUpper(), OperatorType.Like, "%" + q + "%", true);

            //生成对象实例
            object dObj = Activator.CreateInstance(type);
            object ret=null;
            if (string.IsNullOrEmpty(strMethod))
            {
                if (!string.IsNullOrEmpty(limit))
                {
                    if (limit == "0")
                    {
                        //反射方法
                        method = type.GetMethod("FindToDataReader", new Type[] { typeof(WhereFields) });

                        WhereFields[] parameters = new WhereFields[] { whereFields };
                        //调用对象方法
                        ret = method.Invoke(dObj, parameters);
                    }
                    else
                    {
                        int pageSize = Int32.Parse(limit);

                        //反射方法
                        method = type.GetMethod("FindToDataReader", new Type[] { typeof(WhereFields), typeof(Int32), typeof(Int32) });

                        object[] parameters = new object[] { whereFields, 0, pageSize };
                        //调用对象方法
                        ret = method.Invoke(dObj, parameters);
                    }
                }
                else
                {
                    method = type.GetMethod("GetAllToDataReader", Type.EmptyTypes);
                    //调用对象方法
                    ret = method.Invoke(dObj, null);
                }
                //转换成json格式
                if (null != method && null != ret)
                {
                    json = Water.Util.Convert.ToJSONStringFromDataReader((System.Data.IDataReader)(ret));

                }
            }
            else {
                method = type.GetMethod(strMethod);
                ret = method.Invoke(dObj, null);
                if (ret is System.Data.DataSet)
                {
                    json = Water.Util.Convert.ToJSONStringFromDataSet((System.Data.DataSet)(ret));
                    goto aa;
                }
                
                if (ret is System.Data.IDataReader)
                {
                    json = Water.Util.Convert.ToJSONStringFromDataReader((System.Data.IDataReader)(ret));
                    goto aa;
                }
                if (ret is System.Data.DataTable)
                {
                    json = Water.Util.Convert.ToJSONStringFromDataTable((System.Data.DataTable)(ret));
                    goto aa;
                }
                if (ret is System.Collections.IDictionary)
                {
                    json = Water.Util.Convert.ToJSONStringFromDictionary((System.Collections.IDictionary)(ret));
                    goto aa;
                }
                else
                {
                    json = Water.Util.Convert.ToJSONStringFromCustomObect(ret);
                    goto aa;
                }
                
            }
        aa: ;
            context.Response.Write(json);
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
